<template>
  <div style="font-size: 16px;font-weight: 600;"> <el-radio></el-radio>展会列表</div>
  <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
    <el-row style="margin-top: 15px;">
      <el-form-item label="展会名称">
        <el-input v-model="state.dataForm.exhName" type="text" clearable placeholder="请输入" style="width: 220px"></el-input>
      </el-form-item>
      <el-form-item label="开展日期">
        <el-date-picker v-model="state.dataForm.exhBTime" type="date" placeholder="开始日期" style="width: 220px"></el-date-picker>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-date-picker v-model="state.dataForm.exhETime" type="date" placeholder="结束日期" style="width: 220px"></el-date-picker>
      </el-form-item>
      <!-- 查询 -->
      <el-button @click="state.getDataList()" type="primary">{{ $t("query") }}</el-button>
    </el-row>
    <!--新增-->
    <el-form-item>
      <el-button v-if="state.hasPermission('crm:crmexhibition:save')" @click="addOrUpdateHandle()">{{ $t("add")
        }}</el-button>
    </el-form-item>
    <!--编辑-->
    <el-form-item>
      <el-button v-if="state.hasPermission('crm:crmexhibition:update')" @click="addOrUpdateHandle()"
        :disabled="state.single">{{ $t("编辑")
        }}</el-button>
    </el-form-item>
    <!--设置有权人员 -->
    <el-form-item>
      <el-button v-if="state.hasPermission('sys:user:getLeader')" @click="handleAuthority()" :disabled="state.single">设置有权人员</el-button>
    </el-form-item>
    <!-- 设置当前展会 -->
    <el-form-item>
      <el-button  @click="handleCurrent()" :disabled="state.multiple" >设置当前展会</el-button>
    </el-form-item>
    <!-- 删除 -->
    <el-form-item>
      <el-button v-if="state.hasPermission('crm:crmexhibition:delete')" type="danger"
        @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
    </el-form-item>
    <!-- 表格 -->
    <el-table :header-cell-style="{ 'background': '#eef1f6', 'color': '#1f2d3d', 'border-color': '#dfe6ec' }" center
      border v-loading="state.dataListLoading" :data="state.dataList"
      @selection-change="state.dataListSelectionChangeHandle">
      <el-table-column type="selection"></el-table-column>
      <el-table-column align="center" prop="id" label="ID" width="120"></el-table-column>
      <el-table-column align="center" prop="exhSName" label="展会简称" width="250"></el-table-column>
      <el-table-column align="center" prop="exhName" label="展会名称" width="380"></el-table-column>
      <el-table-column align="center" prop="exhCity" label="举办城市" width="150"></el-table-column>
      <el-table-column align="center" prop="exhBTime" label="开始时间"></el-table-column>
      <el-table-column align="center" prop="exhETime" label="结束时间"></el-table-column>
      <el-table-column align="center" label="是否当前" width="120">
        <template v-slot="scope">
          <div v-if="scope.row.ifCurExh == 1">
            <el-icon color="#17b3a3">
              <SuccessFilled />
            </el-icon>
          </div>
          <div v-else></div>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <!-- 页码 -->
  <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
    :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle"
    @current-change="state.pageCurrentChangeHandle"> </el-pagination>
  <!-- 弹窗, 新增 / 修改 -->
  <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  <!-- 设置有权人员 -->
  <power ref="exhibitionPowerRef" @refreshDataList="state.getDataList"></power>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import baseService from "@/service/baseService";
import AddOrUpdate from "./exhibition-list-add-or-update.vue";
import power from "./exhibition-list-power.vue";
import { ElMessage } from "element-plus";
const view = reactive({
  getDataListURL: '/crm/crmexhibition/page',
  getDataListIsPage: true,
  deleteURL: "/crm/crmexhibition",
  deleteIsBatch: true,
  dataForm: {
    id: "",
    state: "",
    exhName: "",
    dataListSelections: [],
    exhBTime: "",//开始日期
    exhETime: "",//结束日期
  },
  dataForm2: {
  },
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const dialogVisible = ref();
dialogVisible.value = false

const addKey = ref(0);
const ida = ref(0);

const addOrUpdateRef = ref();
const exhibitionPowerRef = ref();

/**
 * 编辑
 */
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  if (state.dataListSelections) {
    if (Object.keys(state.dataListSelections).length === 0) {
      nextTick(() => {
        addOrUpdateRef.value.init(id);
      });
    } else {
      ida.value = state.dataListSelections[0].id
      nextTick(() => {
        addOrUpdateRef.value.init(Number(ida.value));
      });
    }
  }
};

/**
 * 设置当前展会
 */
const handleCurrent = () => {
  let ids: string[] = [];
  for (let i = 0; i < state.dataListSelections!.length; i++) {
    ids.push(state.dataListSelections![i].id);
  }
  state.dataForm2 = ids;
  return baseService.put("/sys/user/updateCurExh", state.dataForm2).then((res) => {
    if (res.code == 0) {
      // 重新获取数据列表
      state.getDataList();
      return ElMessage.success("设置当前展会成功");
    }
  });
}

/**
 * 设置有权人员
 */
const handleAuthority = (id?: number) => {
  if (state.dataListSelections) {
    if (Object.keys(state.dataListSelections).length === 0) {
      nextTick(() => {
        exhibitionPowerRef.value.init(id);
      });
    } else {
      ida.value = state.dataListSelections[0].id
      nextTick(() => {
        exhibitionPowerRef.value.init(Number(ida.value));
      });
    }
  }
}
</script>
<style lang="scss" scoped>

::v-deep .el-table .cell {
  white-space: nowrap;
  }
</style>
